$(document).ready(function() {

	//navEvent();

});
//增加 touchstart 事件 阻止click事件
function navEvent() {
	var nav = document.getElementById("defaultTab")
	if(nav != null) {
		nav.addEventListener("touchstart", function(e) {
			e.preventDefault();
			homeTouch() 
		});
		var settings = document.getElementById("settings")
		settings.addEventListener("touchstart", function(e) {
			e.preventDefault();
 
		});
	}

}

function AddData() {
	mui.plusReady(function() {
		plus.nativeUI.showWaiting();
	});

	$.get("http://www.maytao.cn/Home/InitData", function(data) {
		mui.plusReady(function() {
			plus.nativeUI.closeWaiting();
		});
		if(data.Code == 0) {

			var jsonobj = eval(data.Data)
			//记录最新id
			localStorage.fstproid = jsonobj[0].ProductId;
			// console.log(localStorage.fstproid+"+"+jsonobj[0].ProductId)
			var content = AppendHtml(jsonobj);
			$("#content").html(content);
		} else {
			mui.toast("稍后再试");
		}

	});

}

function workToGetNewData() {
	//定时刷新查找新数据
	window.setInterval(function() {
		SetTimeGetData();
	}, 1000 * 60 * 3);
}

//定时获取数据
function SetTimeGetData() {

	$.post("http://www.maytao.cn/Home/GetNewData", {
		id: localStorage.fstproid
	}, function(data) {

		if(data.Code == 0) {
			var jsonobj = eval(data.Data)
			var newDataCount = jsonobj.length;

			$("#sphome").html(newDataCount);
			$("#sphome").css("display", "block");

		}
	});

}
//翻页获取更多数据
function GetMoreData(id, keyword) {
 
	mui.plusReady(function() { 
		plus.nativeUI.showWaiting();
	});

	$.post("http://www.maytao.cn/Home/LoadMoreData", {
			id: id,
			searchKey: keyword
		},
		function(data) {
			mui.plusReady(function() {
				plus.nativeUI.closeWaiting();
			});
			if(data.Code == 0) {

				var content = "";
				var jsonobj = eval(data.Data)

				content = AppendHtml(jsonobj);
				$("#content").append(content);

			} else if(data.Code == -1) {
				isEnd = true;
				mui.toast('扯到底啦');
			} else {
				mui.alert("稍后再试");
			}

		});

}

//获取新数据GetNewData
function GetNewData() {
	mui.plusReady(function() {
		plus.nativeUI.showWaiting();
	});
	$.post("http://www.maytao.cn/Home/GetNewData", {
		id: localStorage.fstproid
	}, function(data) {
		mui.plusReady(function() {
			plus.nativeUI.closeWaiting();
		});
		if(data.Code == 0) {
			//清空角标
			mui.plusReady(function() {
				var instart = plus.webview.currentWebview().opener();
				mui.fire(instart, "HideNotice")
			}) 
			 
			var content = "";
			var jsonobj = eval(data.Data)
			//请求新数据后 更新最新id  
			localStorage.fstproid = jsonobj[0].ProductId;
			content = AppendHtml(jsonobj);

			$("#content").prepend(content);
		} else if(data.Code == -1) {
			mui.toast("已经是最新数据啦");
		} else {
			mui.toast("稍后再试");
		}

	});

}

//获取新数据homeTouch()
function homeTouch() { 
	 
	$("#sphome").html("");
	$("#sphome").css("display", "none");
	mui.plusReady(function() {   
		var instart = plus.webview.getWebviewById("index.html")
		mui.fire(instart, "homeclick")
	})
}

function loadnewdatabyhome() { 
	mui.plusReady(function() {
		plus.nativeUI.showWaiting();
	});
	$.post("http://www.maytao.cn/Home/GetNewData", {
		id: localStorage.fstproid
	}, function(data) {
		mui.plusReady(function() {
			plus.nativeUI.closeWaiting();
		});

		if(data.Code == 0) {
			 ToTop('top');
			var content = "";
			var jsonobj = eval(data.Data)
			//请求新数据后 更新最新id  
			localStorage.fstproid = jsonobj[0].ProductId;
			content = AppendHtml(jsonobj);
			$("#content").prepend(content);
		} else if(data.Code == -1) {
			mui.toast("已经是最新数据啦");
		} else {
			mui.toast("稍后再试");
		}
	});
}
///拼接数据
function AppendHtml(jsonobj) {
	var content = "";
   // localStorage.prolist=jsonobj//缓存数据
	for(var i = 0; i < jsonobj.length; i++) {
		var item = jsonobj[i];
		var name = item.Pname;
 
		content += '<div class="pbox" id=' + item.ProductId + ' onclick="showDetail(this)" ><img src=' + item.ImgUrl + '></img><p style="font-size:14px">' + name + '</p><p></p><p style="color:red;font-size:12px">' + item.Source + '  ' + item.Price + '</p></div>'

	}
	//记录最后一个id
	localStorage.lstproid = jsonobj[jsonobj.length - 1].ProductId;

	return content;
}

//点击事件
function showDetail(obj) {
	var pid = $(obj).attr('id');
	localStorage.pid = pid;
	/*localStorage.imgurl = $(obj).children("img").attr("src"); //图片地址
	var list = $(obj).find('p').each(function(i, itm) {

		if(i == 0) {
			localStorage.prisour = $(itm).html();
		} else if(i == 1) {
			localStorage.title = $(itm).html();
		}
	})*/

	//打开关于页面
	mui.openWindow({
		url: 'prodes.html',
		id: 'prodes'
	});
	//plus.storage.setItem("ll","ll")
	//window.location.href = "detail.html";

}
//问题反馈
$("#btnFeed").click(function() {

});